<?php
function get_xingbie($cid,$comm=0) { //根据身份证号，自动返回性别
 $cid = getIDCard($cid);
 if (!isIdCard($cid)) return '';
 $sexint = (int)substr($cid,16,1);
 if($comm >0){
    return $sexint % 2 === 0 ? '女' : '男';
 }else{
    return $sexint % 2 === 0 ? '女' : '男';
 }
}
function getIDCard($idCard) {
    // 若是15位，则转换成18位；否则直接返回ID
    if (15 == strlen ( $idCard )) {
        $W = array (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1 );
        $A = array ("1","0","X","9","8","7","6","5","4","3","2" );
        $s = 0;
        $idCard18 = substr ( $idCard, 0, 6 ) . "19" . substr ( $idCard, 6 );
        $idCard18_len = strlen ( $idCard18 );
        for($i = 0; $i < $idCard18_len; $i ++) {
            $s = $s + substr ( $idCard18, $i, 1 ) * $W [$i];
        }
        $idCard18 .= $A [$s % 11];
        return $idCard18;
    } else {
        return $idCard;
    }
}
function isIdCard($number) { // 检查是否是身份证号
   $number = getIDCard($number);
   // 转化为大写，如出现x
   $number = strtoupper($number);
   //加权因子
   $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
   //校验码串
   $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
   //按顺序循环处理前17位
   $sigma = 0;
   for ($i = 0;$i < 17;$i++) {
    //提取前17位的其中一位，并将变量类型转为实数
    $b = (int) $number{$i};
 
    //提取相应的加权因子
    $w = $wi[$i];
 
    //把从身份证号码中提取的一位数字和加权因子相乘，并累加
    $sigma += $b * $w;
   }
   //计算序号
   $snumber = $sigma % 11;
 
   //按照序号从校验码串中提取相应的字符。
   $check_number = $ai[$snumber];
 
   if ($number{17} == $check_number) {
    return true;
   } else {
    return false;
   }
}
function ageVerification($code){
    $age_time = strtotime(substr($code, 6, 8));
    if($age_time === false){
        return false;
    }
    list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age_time)); 
 
    $now_time = strtotime("now");
 
    list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now_time));
    $age = $y2 - $y1;
    if((int)($m2.$d2) < (int)($m1.$d1)){
        $age -= 1;
    }
    return $age; 
}
?>